package com.arethusa;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import simpleorm.core.SValidationException;
import simpleorm.ui.SUIConstants;

import com.arethusa.officialdb.GefungeerdinBean;

public class Fungeren extends JDialog implements SUIConstants, WindowListener, ItemListener {

	private static final long serialVersionUID = 1L;

	private boolean gevraagd = false;
	private boolean gefungeerd = false;
	private boolean newRecord = false;
	private JDialog me;
	
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JButton wijzigButton = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel3 = null;
	private JTextField redenTextField = null;
	private JCheckBox gevraagdCheckBox = null;
	private JCheckBox gefungeerdCheckBox = null;
	private JLabel jLabel2 = null;
	private JComboBox funktieComboBox = null;
	private JComboBox jaarComboBox = null;

	private JTextField idTextField = null;
	
	private class WijzigActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if( (gefungeerd && ((String)funktieComboBox.getSelectedItem()).length()>0) ||
					(!gefungeerd && ((String)funktieComboBox.getSelectedItem()).length()==0) ) {
				GefungeerdinBean f = new GefungeerdinBean();
				f.setOfficialid(new Long(idTextField.getText()));
				f.setReden(redenTextField.getText());
				f.setFunktie((String)funktieComboBox.getSelectedItem());
				f.setGevraagd(new Boolean(gevraagd));
				f.setGefungeerd(new Boolean(gefungeerd));
				try {
					f.setJaar(new Long((String)jaarComboBox.getSelectedItem()));
					if(newRecord){
						f.create();
					} else {
						f.update();
					}
					me.setVisible(false);
				} catch (SValidationException e1) {
					JOptionPane.showMessageDialog(null,
						e1.getMessage(),
						"Validatie", JOptionPane.ERROR_MESSAGE);
				} catch (NumberFormatException e2) {
					JOptionPane.showMessageDialog(null,
						"Jaar moet een nummer zijn.",
						"Validatie", JOptionPane.ERROR_MESSAGE);
				}
			} else if( gefungeerd ) {
				JOptionPane.showMessageDialog(null,
						"Als official gefungeerd heeft, moet ook zijn Funktie gevuld worden.",
						"Validatie", JOptionPane.ERROR_MESSAGE);
			} else if( ((String)funktieComboBox.getSelectedItem()).length()>0 ){
				JOptionPane.showMessageDialog(null,
						"Als de Funktie van de official gevuld is, heeft hij ook gefungeerd.",
						"Validatie", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	/**
	 * @param owner
	 */
	public Fungeren(Frame owner) {
		super(owner);
		me = this;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(302, 187);
		this.setModal(true);
		this.setTitle("Fungering");
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public void setFungering( GefungeerdinBean d ) {
		getIdTextField().setText(d.getOfficialid().toString());
		newRecord = (d.getJaar().longValue()==0L);
		if( newRecord ){
			getJaarComboBox().setSelectedItem(
					new Long(Calendar.getInstance().get(Calendar.YEAR)).toString());
		} else {
			getJaarComboBox().setSelectedItem(d.getJaar().toString());
		}
		getGevraagdCheckBox().setSelected(d.getGevraagd().booleanValue());
		getGefungeerdCheckBox().setSelected(d.getGefungeerd().booleanValue());
		getFunktieComboBox().setSelectedItem(d.getFunktie());
		getRedenTextField().setText(d.getReden());
		
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints21.gridy = 1;
			gridBagConstraints21.weightx = 1.0;
			gridBagConstraints21.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints21.ipadx = 50;
			gridBagConstraints21.gridx = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.ipadx = 40;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints9.gridy = 3;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.ipadx = 30;
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.gridy = 3;
			jLabel2 = new JLabel();
			jLabel2.setText("Funktie");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridy = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridy = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.fill = GridBagConstraints.BOTH;
			gridBagConstraints8.gridy = 4;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.gridx = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints7.gridy = 4;
			jLabel3 = new JLabel();
			jLabel3.setText("Reden");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Jaar");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(jLabel, gridBagConstraints);
			jContentPane.add(getWijzigButton(), gridBagConstraints2);
			jContentPane.add(jLabel1, gridBagConstraints3);
			jContentPane.add(jLabel3, gridBagConstraints7);
			jContentPane.add(getRedenTextField(), gridBagConstraints8);
			jContentPane.add(getGevraagdCheckBox(), gridBagConstraints4);
			jContentPane.add(getGefungeerdCheckBox(), gridBagConstraints6);
			jContentPane.add(jLabel2, gridBagConstraints5);
			jContentPane.add(getFunktieComboBox(), gridBagConstraints9);
			jContentPane.add(getJaarComboBox(), gridBagConstraints1);
			jContentPane.add(getIdTextField(), gridBagConstraints21);
		}
		return jContentPane;
	}

	public void windowActivated(WindowEvent arg0) {}
	public void windowClosed(WindowEvent e) {}
	public void windowClosing(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowOpened(WindowEvent e) {}

	/**
	 * This method initializes wijzigButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getWijzigButton() {
		if (wijzigButton == null) {
			wijzigButton = new JButton();
			wijzigButton.setText("Wijzig");
			wijzigButton.addActionListener(new WijzigActionListener());
		}
		return wijzigButton;
	}

	/**
	 * This method initializes redenTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getRedenTextField() {
		if (redenTextField == null) {
			redenTextField = new JTextField();
		}
		return redenTextField;
	}

	/**
	 * This method initializes gevraagdCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getGevraagdCheckBox() {
		if (gevraagdCheckBox == null) {
			gevraagdCheckBox = new JCheckBox();
			gevraagdCheckBox.setText("gevraagd");
			gevraagdCheckBox.addItemListener(this);
		}
		return gevraagdCheckBox;
	}

	/**
	 * This method initializes gefungeerdCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getGefungeerdCheckBox() {
		if (gefungeerdCheckBox == null) {
			gefungeerdCheckBox = new JCheckBox();
			gefungeerdCheckBox.setText("gefungeerd");
			gefungeerdCheckBox.addItemListener(this);
		}
		return gefungeerdCheckBox;
	}

	/**
	 * This method initializes funktieComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getFunktieComboBox() {
		if (funktieComboBox == null) {
			funktieComboBox = new JComboBox();
			funktieComboBox.setPreferredSize(new Dimension(31, 20));
			funktieComboBox.addItem("");
			funktieComboBox.addItem("HS");
			funktieComboBox.addItem("SR");
			funktieComboBox.addItem("KB");
			funktieComboBox.addItem("KA");
			funktieComboBox.addItem("HJ");
			funktieComboBox.addItem("JS");
			funktieComboBox.addItem("TW");
		}
		return funktieComboBox;
	}

	/**
	 * This method initializes jaarComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	public JComboBox getJaarComboBox() {
		if (jaarComboBox == null) {
			jaarComboBox = new JComboBox();
			jaarComboBox.setPreferredSize(new Dimension(31, 20));
			for(int i=1990; i<2050; i++) {
				jaarComboBox.addItem(""+i);
			}
		}
		return jaarComboBox;
	}

	public void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();

        if (source == getGevraagdCheckBox()) {
            gevraagd = (e.getStateChange() == ItemEvent.SELECTED);
        } else if (source == getGefungeerdCheckBox()) {
            gefungeerd = (e.getStateChange() == ItemEvent.SELECTED);
        }
		
	}

	/**
	 * This method initializes idTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getIdTextField() {
		if (idTextField == null) {
			idTextField = new JTextField();
			idTextField.setVisible(false);
		}
		return idTextField;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
